﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace cebao.Web.devinfo
{
    /// <summary>
    /// GetDev 的摘要说明
    /// </summary>
    public class GetDev : IHttpHandler, IRequiresSessionState
    {
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        cebao.BLL.devinfo bll = new BLL.devinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "querycbd")
                Querycbd(context);
            else if (ac == "querypic")
                Querypic(context);
            else if (ac == "queryMQ_QJ")
                QueryMQ_QJ(context);
            else if (ac == "queryuid")
                Queryuid(context);
            else if (ac == "queryxqh")
                Queryxqh(context);
            else if (ac == "querycbd1")
                Querycbdone(context);

        }
        public void Querycbdone(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                string ybqname = "", ybqid = "";
                string devid = context.Request.QueryString["devid"];

                ybqname = bll.getYBQ(devid, "PIC");
                if (ybqname.Contains(","))
                {
                    ybqid = ybqname.Substring(0, ybqname.IndexOf(","));
                    ybqname = ybqname.Substring(ybqname.IndexOf(",") + 1);
                    string json = "[{\"ybqid\":\"" + ybqid + "\",\"ybqch\":\"" + ybqname + "\"}]";
                    context.Response.Write(json);
                    context.Response.End();
                }
                else
                {
                    string json = "[{\"cbdid\":\"" + ybqname + "\"}]";
                    context.Response.Write(json);
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Queryxqh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                string ybqname = "", ybqid = "";
                string devid = context.Request.QueryString["devid"];
                ybqname = bll.getYBQ(devid, "PICSB");
                if (ybqname.Contains(","))
                {
                    ybqid = ybqname.Substring(0, ybqname.IndexOf(","));
                    ybqname = ybqname.Substring(ybqname.IndexOf(",") + 1);
                    string json = "[{\"ybqid\":\"" + ybqid + "\",\"ybqch\":\"" + ybqname + "\"}]";
                    context.Response.Write(json);
                    context.Response.End();
                }
                else
                {
                    string json = "[{\"ybqid\":\"\",\"ybqch\":\"\"}]";
                    context.Response.Write(json);
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Queryuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                
                string devtype="", st = "";
                int uid = 0;
                //获取前台传来的值 
                if (null != context.Request.QueryString["uid"])
                {//获取前台传来的值  
                    st = context.Request.QueryString["uid"].ToString().Trim();
                    int.TryParse(st, out uid);
                }
                if (null != context.Request.QueryString["devtype"])
                {//获取前台传来的值  
                    devtype = context.Request.QueryString["devtype"].ToString().Trim();
                    
                }
                StringBuilder strWhere = new StringBuilder();
                
                strWhere.AppendFormat(" DevOwnerID='{0}' and DevType='{1}'", uid,devtype);                
                DataSet ds = bll.GetList(strWhere.ToString());
                int count = 0;
                if(ds!=null)
                count = ds.Tables[0].Rows.Count;//获取条数  
                string strJson = GetTreeJsonByTableOne(ds.Tables[0], "DevContact", "DevID", "DevOwnerName"); ;//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"iconCls\": \"icon-bullet_green\",\"bm\":\"" + row[bmCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void QueryMQ_QJ(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, uprovince, ucity, uunit, ucounty, st = "";
                utruename = uname = uprovince = ucity = uunit = ucounty = "";
                if (level == -1) level = 3;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (uprovince != "")
                {
                    st = uprovince;

                }

                if (ucity != "")
                {
                    st += ucity;
                }
                if (ucounty != "")
                {
                    st += ucounty;
                }
                if (st != "")
                {
                    strWhere.AppendFormat(" madd like '%{0}%' and ", st);
                }
                strWhere.AppendFormat(" DevType='MQ_QJ'", st);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querypic(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, uprovince, ucity, uunit, ucounty, st = "";
                utruename = uname = uprovince = ucity = uunit = ucounty = "";
                if (level == -1) level = 3;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (uprovince != "")
                {
                    st = uprovince;

                }

                if (ucity != "")
                {
                    st += ucity;
                }
                if (ucounty != "")
                {
                    st += ucounty;
                }
                if (st != "")
                {
                    strWhere.AppendFormat(" madd like '%{0}%' and ", st);
                }
                strWhere.AppendFormat(" DevType='PIC'", st);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querycbd(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, uprovince, ucity, uunit, ucounty, st = "";
                utruename = uname = uprovince = ucity = uunit = ucounty = "";
                if (level == -1) level = 3;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (uprovince != "")
                {
                    st = uprovince;

                }

                if (ucity != "")
                {
                    st += ucity;
                }
                if (ucounty != "")
                {
                    st += ucounty;
                }
                if (st != "")
                {
                    strWhere.AppendFormat(" madd like '%{0}%' and ", st);
                }
                strWhere.AppendFormat(" DevType='CBD'", st);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string uname, utruename, uprovince, ucity, uunit, ucounty, st = "";
                utruename = uname = uprovince = ucity = uunit = ucounty = "";
                if (level == -1) level = 3;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" UserName like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" TrueName like '%{0}%' and ", utruename);
                }
                if (level != 0)//不是普通用户
                {
                    if (level <= 3)
                        uprovince = pcc[0];
                    if (level <= 2)
                    {
                        ucity = pcc[1];
                    }
                    if (level <= 1)
                    {
                        ucounty = pcc[2];
                    }
                    if (uprovince != "")
                    {
                        st = uprovince;
                    }
                    if (ucity != "")
                    {
                        st += ucity;
                    }
                    if (ucounty != "")
                    {
                        st += ucounty;
                    }
                    if (st != "")
                    {
                        strWhere.AppendFormat(" f_name like '%{0}%' and ", st);
                    }
                }
                else
                {
                    strWhere.AppendFormat(" UserName='{0}' and ", Web.Admin.myinfo.getName(context));

                }
                strWhere.AppendFormat(" DevType='XQH'", st);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}